/*
 * 人脸验证
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
*/
layui.extend({
  setter: 'config' //配置模块
  ,signature: 'lib/extend/signature' //人脸识别
}).define(['setter','signature'],function(exports){ //提示：模块也可以依赖其它模块，

var setter = layui.setter;
var $ = layui.jquery;

var sign={

	/*
     * 获取权限数据
     * 参数解释：
       url   
    */
    render:function($this, options){
       
        var divID;
        var that=this,height,width;
        

        this.divID = $this;
        this.height = options.height ? options.height : 380;
        this.width  = options.width ? options.width :980;
        this.data = options.data ? options.data : [];
        this.url = options.url ? options.url : '';
        this.submit = options.submit ? options.submit : 'submit';
        
        that.resize();

        (function($) {
        var topics = {};
        $.publish = function(topic, args) {
        if (topics[topic]) {
        var currentTopic = topics[topic],
        args = args || {};
        for (var i = 0, j = currentTopic.length; i < j; i++) {
        currentTopic[i].call($, args);
        }
        }
        };
      $.subscribe = function(topic, callback) {
        if (!topics[topic]) {
        topics[topic] = [];
        }
        topics[topic].push(callback);
        return {
        "topic": topic,
        "callback": callback
        };
        };
      $.unsubscribe = function(handle) {
        var topic = handle.topic;
        if (topics[topic]) {
        var currentTopic = topics[topic];
        for (var i = 0, j = currentTopic.length; i < j; i++) {
        if (currentTopic[i] === handle.callback) {
        currentTopic.splice(i, 1);
        }
        }
        }
        };
    })(jQuery);
    
    (function($){
      $(document).ready(function() {
      var $sigdiv = $("#signature").jSignature({'UndoButton':true})
      $('#signbtn').bind('click', function(e){
      var data = $sigdiv.jSignature('getData','default');
      if(typeof data === 'string'){
      $('#signuppic').val(data);
      $('#content').hide();
      $('#signpic').show();
      $('#signpic').append("<img src='"+data+"'>");
      $('#signsub').show();
      $('#signbtn').hide();
      }else{
      try{
      $('#signuppic').val(JSON.stringify(data));
      } catch (ex) {
      alert('错误，格式化二进制失败');
      }
      }
      })
      $('#signreset').bind('click', function(e){
      $sigdiv.jSignature('reset');
      $('#signuppic').val('');
      $('#content').show();
      $('#signpic').hide();
      $('#signpic').html('');
      $('#signsub').hide();
      $('#signbtn').show();
      })
      })
      })(jQuery)
    
    },

    // 设置相关容器
    resize:function() {
        const wraper = document.querySelector(this.divID);
        wraper.setAttribute("style", 'width:'+`${this.width}px`+'; height:'+ `${this.height}px`+';position: absolute; left:50%;right: 50%;transform: translateX(-50%);display: flex;align-items: center;justify-content: center;');
       
        const divc=document.createElement("div");
        divc.setAttribute("class", 'layui-card');
        divc.setAttribute("style", 'width:'+`${this.width}px`+'; height:'+ `${this.height}px`+';');
        var content = '<div class="layui-card-body" style="padding: 15px;">'+
                '<div class="layui-form-item" style="text-align: center;"><b>签字版</b></div>'+
                '<form name="addItem" method="post" action="'+this.url+'" lay-filter="component-form-group" class="layui-form">'+
                '<input type="hidden" value="'+this.data.id+'" name="id">'+
                '<input type="hidden" value="'+this.data.uid+'" name="uid">'+
                '<input type="hidden" id="signuppic" value="" name="signuppic">'+
                '<div id="content">'+
                '<div id="signatureparent">'+
                '<div id="signature"></div></div>'+
                '</div>'+
                '<div id="signpic" class="signatured" style="display: none;"></div>'+
                '<div class="layui-form-item layui-layout-admin">'+
                    '<div class="layui-input-line">'+
                      '<div class="layui-footer" style="left: 0;">'+
                        '<button type="button" class="layui-btn" id="signbtn">确认</button>'+
                        '<button type="submit" class="layui-btn" id="signsub" style="display: none;" lay-submit lay-filter="'+this.submit+'">确认</button>'+
                        '<button type="button" class="layui-btn layui-btn-primary" id="signreset">重置</button>'+
                        '<button type="button" class="layui-btn layui-btn-primary" onClick="xadmin.close();" >返回</button>'+
                      '</div>'+
                    '</div>'+
                  '</div>'+
                '</form>'+
              '</div>';
        divc.innerHTML=content;
        wraper.append(divc);

    }

}  
exports('sign', sign);
});
